<template>
  <a-modal :title="title" :width="800" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen
    @ok="handleOk" @cancel="handleCancel" cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="人员">
          <a-select show-search :filter-option="filterOption" ref="select" @select="selectUser" placeholder='请选择人员'
            v-model="realName">
            <a-select-option v-for="(statusText, statusValue) in zcdUsers" :key="statusValue" :value="statusValue">
              {{ statusText.phone }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="司机姓名">
          <a-input placeholder="请输入司机姓名" v-model="driverName" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="司机联系方式">
          <a-input placeholder="请输入司机联系方式" v-model="driverPhone" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="车牌号">
          <a-input placeholder="请输入车牌号" v-model="carNum" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="炼厂">
          <a-select show-search  :filter-option="filterOption1" ref="select" @select="selectLc" placeholder='请选择炼厂'
            >
            <a-select-option v-for="(statusText, statusValue) in lcLists" :key="statusValue" :value="statusValue">
              {{ statusText.refineryName+"--"+statusText.city }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    httpAction,
    getAction
  } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "YsySealCarOrderModal",
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },
        realName: "",
        userId: "",
        driverName: "",
        driverPhone: "",
        carNum: "",
        refineryId: "",
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        refineryCity: '',
        url: {
          add: "/app/ysySealCarOrder/adminAdd",
          edit: "/app/ysySealCarOrder/adminAdd",
          zcdList: "/app/ysyUsers/userList",
          lcList: "/app/ysyRefinery/list"
        },
        zcdUsers: [],
        lcLists: []
      }
    },
    created() {
      this.zcdList()
      this.getLcList()
    },
    methods: {
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      filterOption1(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      selectLc(e) {
        this.refineryId = this.lcLists[e].refineryId
        this.refineryCity = this.lcLists[e].refineryName + "--" + this.lcLists[e].refineryName
      },
      getLcList() {
        let params = {
          pageNo: 1,
          pageSize: 999999
        }
        getAction(this.url.lcList, params).then((res) => {
          this.lcLists = res.result.records
        }).finally(() => {})
      },
      selectUser(e) {
        this.realName = this.zcdUsers[e].phone
        this.userId = this.zcdUsers[e].id
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'refineryAddress', 'refineryLat', 'refineryLng', 'refineryId',
            'carNum', 'driverName', 'driverPhone', 'refineryName', 'status', 'commission', 'additionalPrice',
            'userId', 'incomingUserId', 'incomingUserName', 'incomingUserPhone', 'incomingUserAvatar',
            'sealUserFlag', 'transferFlag', 'transferUserId', 'transferUserName', 'transferUserPhone',
            'transferUserAvatar', 'transferStatus'))
          //时间格式化
          this.form.setFieldsValue({
            incomingTime: this.model.incomingTime ? moment(this.model.incomingTime) : null
          })
          this.form.setFieldsValue({
            transferTime: this.model.transferTime ? moment(this.model.transferTime) : null
          })
        });

      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      zcdList() {
        let params = {
          pageNo: 1,
          pageSize: 999999
        }
        getAction(this.url.zcdList, params).then((res) => {
          this.zcdUsers = res.result.records
        }).finally(() => {})
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);

            formData.userId = this.userId
            formData.refineryId = this.refineryId
            formData.driverName = this.driverName
            formData.carNum = this.carNum
            formData.driverPhone = this.driverPhone

            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel() {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>